﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using System.Collections.Generic;
using Dict.Blo;

namespace UI
{
    public class VsGetItem : MonoBehaviour
    {
        public UISprite tipIcon;
        public UILabel progress;
        public UILabel winTimes;
        public UILabel getNum;
        public UILabel getName;
        public GameObject commonIcon;
        public GameObject btnGo;
        public GameObject btnGet;
        public GameObject imgGetted;

        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public delegate void UpdateList(List<ActivityItem> list);
        public UpdateList m_update;

        public delegate void AddRedNum();
        public AddRedNum m_addNum;

        public ActivityType type;
        public ActivityType tabType;
        int oneOrAll;
        string activityId;
        int itemIndex;
        List<BaseResource> getRes = new List<BaseResource>();
        public void SetActivityItem(ActivityItem activityItem, string acId)//单个物体
        {
            activityId = acId;
            itemIndex = activityItem.index;
            //奖励
            commonIcon.GetComponent<CommonIcon>().SetData(activityItem.rewardList[0].id, true);
            getNum.text = activityItem.rewardList[0].num.ToString();
            getName.text = TableTextBlo.GetText(CommonMethod.GetObjectNameTextId(activityItem.rewardList[0].id));
            winTimes.text = string.Format(GetTextString(), activityItem.value); 
            progress.text = activityItem.left.ToString() + "/" + activityItem.total;
            oneOrAll = activityItem.pickOneOrGetAll;
            getRes = activityItem.rewardList;
            //领奖状态
            btnGo.SetActive(activityItem.status == 0);
            btnGet.SetActive(activityItem.status == 1);
            imgGetted.SetActive(activityItem.status == 2);
            if (activityItem.status == 1 && m_addNum != null)
                m_addNum();
        }

        public void SetActivityMultiItem(ActivityItem activityItem, string acId)//多个物体
        {
            activityId = acId;
            itemIndex = activityItem.index;
            //奖励
            foreach (var i in activityItem.rewardList)
            {
                GameObject obj = Instantiate(commonIcon);
                obj.SetActive(true);
                obj.GetComponent<CommonIcon>().SetData(i.id, true, i.num);
                simpleGrid.AddChild(obj);
            }
            winTimes.text = string.Format(GetTextString(), activityItem.value);
            progress.text = activityItem.left.ToString() + "/" + activityItem.total;
            oneOrAll = activityItem.pickOneOrGetAll;
            if (activityItem.rewardList.Count <= 3)
                scrollView.enabled = false;
            getRes = activityItem.rewardList;
            //领奖状态
            btnGo.SetActive(activityItem.status == 0);
            btnGet.SetActive(activityItem.status == 1);
            imgGetted.SetActive(activityItem.status == 2);
            if (activityItem.status == 1 && m_addNum != null)
                m_addNum();
        }

        string GetTextString()
        {
            switch (type)
            {
                case ActivityType.BattleRace:
                    tipIcon.spriteName = "UI_item_title_jignjichang";
                    return TableTextBlo.GetText("BattleRace_WinTimes_Label");
                case ActivityType.RobTrea:
                    tipIcon.spriteName = "UI_item_title_duobaosongli";
                    return TableTextBlo.GetText("RobTrea_getNum_Limit_tips");
                case ActivityType.PrizeTask:
                    tipIcon.spriteName = "UI_item_title_panjungongda";
                    return TableTextBlo.GetText("PrizeTask_getNum_Limit_tips");
                case ActivityType.PrizeBoss:
                    tipIcon.spriteName = "UI_item_title_panjunjisha";
                    return TableTextBlo.GetText("PrizeBoss_getNum_Limit_tips");
                case ActivityType.MainCopy:
                    tipIcon.spriteName = "UI_item_tab_10_a";
                    return TableTextBlo.GetText("copy_award_getNum_limit_tips");
                case ActivityType.AincradStars:
                    tipIcon.spriteName = "UI_item_title_shilianzhidi";
                    return TableTextBlo.GetText("practice_star_numlimit_tips");
                default:
                    Debug.LogError("类型有问题哟~" + type);
                    return "";
            }
        }

        public void OnBtnGo()
        {
            if (StaticData.playerData.IsHeroFull())
            {
                BoxManager.ShowHeroFull();
                return;
            }
            if (StaticData.playerData.IsEquipFull())
            {
                BoxManager.ShowEquipFull();
                return;
            }
            //您当前位于监狱中，不能进行该操作
            if (StaticData.isPlayerNeedJail)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("prison_you_cannot_do"));
                return;
            }
            if(tabType != ActivityType.None)
                GetComponentInParent<UIActivity>().options["tab"] = ((int)tabType).ToString();
            else
                GetComponentInParent<UIActivity>().options["tab"] = ((int)type).ToString();
            switch (type)
            {
                case ActivityType.BattleRace:
                    if (StaticData.playerData.level < DictConfigBlo.GetRankPVPLimitLevel())
                    {
                        var str = string.Format(TableTextBlo.GetText("x_level_open"), Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.PVPLevel);
                        BoxManager.CreatePopupTextBox(str);
                        return;
                    }
                    PageManager.Instance.OpenPage("ArenaPage", "type=0");
                    break;
                case ActivityType.RobTrea:
                    if (StaticData.playerData.level < DictConfigBlo.GetPlunderPVPLimitLevel())
                    {
                        var str = string.Format(TableTextBlo.GetText("x_level_open"), Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.RobTreasureLevel);
                        BoxManager.CreatePopupTextBox(str);
                        return;
                    }
                    PageManager.Instance.OpenPage("ArenaPage", "type=1");
                    break;
                case ActivityType.PrizeTask:
                case ActivityType.PrizeBoss:
                    if (StaticData.playerData.level < DictConfigBlo.GetSailingLevelLimitLevel())
                    {
                        var str = string.Format(TableTextBlo.GetText("x_level_open"), Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.WantedMissionLevel);
                        BoxManager.CreatePopupTextBox(str);
                        return;
                    }
                    PageManager.Instance.OpenPage("MercenaryTaskPage", "");
                    break;
                case ActivityType.AincradStars:
                    if (StaticData.playerData.level < DictConfigBlo.GetINCarnatingLimitLevel())
                    {
                        var str = string.Format(TableTextBlo.GetText("x_level_open"), Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.INCarnatingLevel);
                        BoxManager.CreatePopupTextBox(str);
                        return;
                    }
                    PageManager.Instance.OpenPage("RushPage", ""); 
                    break;
                case ActivityType.MainCopy:
                    PageManager.Instance.OpenPage("UICopy", "");
                    break;
                default:
                   break;
            }
           
        }

        public void OnBtnGet()
        {
            if (oneOrAll == 1)
            {
                CSActivityReward msg = new CSActivityReward();
                msg.activityId = this.activityId;
                msg.index = itemIndex;
                msg.getResources.AddRange(getRes);
                NetAPI.SendHttp(OpDefine.CSActivityReward, msg, GetRewardCallback);
            }
            else if (oneOrAll == 0)
            {
                PageManager.Instance.OpenPage("SelectAcRewardPage", "");
                SelectAcRewardPage reward = PageManager.Instance.GetDictPage("SelectAcRewardPage") as SelectAcRewardPage;
                reward.m_click = this.SelectAcAward;
                reward.SetReward(getRes);
            }
        }

        void SelectAcAward(BaseResource item)
        {
            CSActivityReward msg = new CSActivityReward();
            msg.activityId = this.activityId;
            msg.index = itemIndex;
            for (int i = 0; i < getRes.Count; i++)
            {
                if (getRes[i].id == item.id)
                {
                    msg.getResources.Add(getRes[i]);
                    break;
                }
            }
            NetAPI.SendHttp(OpDefine.CSActivityReward, msg, GetRewardCallback);
        }

        void GetRewardCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityReward msg = packet.kBody as SCActivityReward;
                StaticData.playerData.AddResultObj(msg.get);
                CommonMethod.ShowGetObjs(msg.get);
                m_update(msg.itemList);
            }
        }
    }
}

